clc; close all; clear;
addpath('../02_Functions');


%------------------------------------------------------------------------%
% This file creates figure 6(a) in the paper 
%------------------------------------------------------------------------%


% QUOTES
file_name = '../01_data/es_quotes_5minute_EST';

% choose sample period
start_date            = datenum(1998,1,5);
finish_date           = datenum(2020,12,29);

% Compute returns
winsorizing           = 0;   
alpha                 = 0.99;       
replace_option        = 1; 

results               = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
caldt                 = results.caldt;
log_return_AD         = results.log_return_AD;

% Hours;
H1 = 7;
H2 = 8;
H3 = 9;
H4 = 10;

log_rets_H1           = sum(log_return_AD(:,12*(H1-1)+1:12*H1),2);
log_rets_H2           = sum(log_return_AD(:,12*(H2-1)+1:12*H2),2);
log_rets_H3           = sum(log_return_AD(:,12*(H3-1)+1:12*H3),2);
log_rets_H4           = sum(log_return_AD(:,12*(H4-1)+1:12*H4),2);
log_rets_H5           = sum(log_return_AD(:,12*(H1-1)+1:12*H3),2);
log_rets_H6           = sum(log_return_AD(:,12*(H1-1)+1:12*H4),2);

rets_H1               = exp(log_rets_H1)-1;
rets_H2               = exp(log_rets_H2)-1;
rets_H3               = exp(log_rets_H3)-1;
rets_H4               = exp(log_rets_H4)-1;
rets_H5               = exp(log_rets_H5)-1;
rets_H6               = exp(log_rets_H6)-1;

cum_rets_H1           = cumprod(1+rets_H1);
cum_log_rets_H1       = cumsum(log_rets_H1);

cum_rets_H2           = cumprod(1+rets_H2);
cum_log_rets_H2       = cumsum(log_rets_H2);

cum_rets_H3           = cumprod(1+rets_H3);
cum_log_rets_H3       = cumsum(log_rets_H3);

cum_rets_H4           = cumprod(1+rets_H4);
cum_log_rets_H4       = cumsum(log_rets_H4);

cum_rets_H5           = cumprod(1+rets_H5);
cum_log_rets_H5       = cumsum(log_rets_H5);

cum_rets_H6           = cumprod(1+rets_H6);
cum_log_rets_H6       = cumsum(log_rets_H6);

% number for text 
log_return_H1_end       = cum_log_rets_H1(end);
log_return_H2_end       = cum_log_rets_H2(end);
log_return_H3_end       = cum_log_rets_H3(end);
log_return_H4_end       = cum_log_rets_H4(end);
log_return_H5_end       = cum_log_rets_H5(end);
log_return_H6_end       = cum_log_rets_H6(end);

return_H1_end           = exp(log_return_H1_end);
return_H2_end           = exp(log_return_H2_end);
return_H3_end           = exp(log_return_H3_end);
return_H4_end           = exp(log_return_H4_end);
return_H5_end           = exp(log_return_H5_end);
return_H6_end           = exp(log_return_H6_end);

str_H1                  = ['\leftarrow $',num2str(return_H1_end,'%2.2f')];
str_H2                  = ['\leftarrow $',num2str(return_H2_end,'%2.2f')];
str_H3                  = ['\leftarrow $',num2str(return_H3_end,'%2.2f')];
str_H4                  = ['\leftarrow $',num2str(return_H4_end,'%2.2f')];
str_H5                  = ['\leftarrow $',num2str(return_H5_end,'%2.2f')];
str_H6                  = ['\leftarrow $',num2str(return_H6_end,'%2.2f')];


% -cum returns ----------------------------------

LW = 4;
FS = 18;

figure;
plot(caldt , cum_log_rets_H1,'Linestyle','-','Linewidth',LW,'Color',rgb('green'));
hold on
plot(caldt , cum_log_rets_H2,'Linestyle','-','Linewidth',LW,'Color',rgb('blue'));
plot(caldt , cum_log_rets_H3,'Linestyle','-','Linewidth',LW,'Color',rgb('red'));
plot(caldt , cum_log_rets_H5,'Linestyle','-','Linewidth',LW,'Color',rgb('purple'));
text(finish_date,cum_log_rets_H1(end)+0.04 , str_H1 ,'FontSize',FS,'color','black')
text(finish_date,cum_log_rets_H2(end)-0.04 , str_H2 ,'FontSize',FS,'color','black')
text(finish_date,cum_log_rets_H3(end) , str_H3 ,'FontSize',FS,'color','black')
text(finish_date,cum_log_rets_H5(end) , str_H5 ,'FontSize',FS,'color','black')
hold on
legend('00:00-01:00', '01:00-02:00' , '02:00-03:00' , '00:00-03:00','Location','NorthWest')
xlabel('Year' )
ylabel('Cumulative Returns to $1 Investment' )
datetick('x',11)
xlim([min(caldt)-66 max(caldt)+66])

